import request from "@/utils/request";
import {PagingModel} from "@/model/PagingModel";
import {QueueRegItemModel} from "@/model/QueueRegItemModel";
import {QueueItemEmplModel} from "@/model/QueueItemEmplModel";

export default class RegItemApi {

    public static downloadTemplate(): Promise<void> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/download/template`,
                responseType: 'blob',
                method: 'get',
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                },
            }).then((res: any) => {
                let b = new Blob([res]);     //拿到文件流下载对象
                let url = window.URL.createObjectURL(b);  //生成文件流下载链接
                let a = document.createElement('a');      //创建一个a标签用来跳转
                a.href = url;                             // a标签 href  赋值 链接
                a.style.display = 'none'
                a.download = '科目导入模板.xlsx';                  //设置下载文件的文件名和文件格式
                document.body.appendChild(a);             //将标签DOM，放置页面
                a.click();
                window.URL.revokeObjectURL(url);     //释放 url 对象内存
                document.body.removeChild(a);//移除标签节点
                resolve()
            }, () => reject())
        })
    }

    /**
     * 查询类目列表
     *
     * @param pageIndex
     * @param pageSize
     */
    public static listRegItem(pageIndex: number, pageSize: number, itemName: string): Promise<PagingModel<QueueRegItemModel>> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/reg_item/list',
                method: 'get',
                params: {
                    pageNum: pageIndex,
                    pageSize,
                    searchKey: itemName,
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    const result = {
                        data: [],
                        pageSize: 0,
                        pageIndex: 1,
                        total: 0
                    }
                    result.data = res.data.rows.map(item => new QueueRegItemModel().deserialize(item))
                    result.pageIndex = pageIndex
                    result.pageSize = pageSize
                    result.total = +res.data.total
                    resolve(result)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }


    public static listAllRegItem(searchKey: string = null, emplId: string = null): Promise<QueueRegItemModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/reg_item/all',
                method: 'get',
                params: {
                    searchKey,
                    emplId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new QueueRegItemModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static listAllSysDept(searchKey: string = null, emplId: string = null): Promise<QueueRegItemModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: '/system/dept/list',
                method: 'get',
                params: {
                    searchKey,
                    emplId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new QueueRegItemModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static saveRegItem(itemModel: QueueRegItemModel): Promise<QueueRegItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/reg_item/save',
                method: 'POST',
                data: itemModel
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueRegItemModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static updateRegItem(id: string, itemModel: QueueRegItemModel): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/${id}`,
                method: 'PUT',
                data: itemModel
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject()
                }
            }, ()=> {reject()})
        })
    }

    public static deleteRegItem(id: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/${id}`,
                method: 'DELETE',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject()
                }
            }, ()=> {reject()})
        })
    }


    public static listEmployeeRegItem(employeeId: string): Promise<QueueRegItemModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/list/employee/${employeeId}`,
                method: 'GET'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data?.map(item => new QueueItemEmplModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static listEmployeeByItem(itemId: string, deptId: string): Promise<QueueItemEmplModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/${itemId}/dept/${deptId}/employee/list`,
                method: 'GET'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new QueueItemEmplModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }



    public static addEmployeeToItem(deptId: string, itemId: string, emplItems: QueueItemEmplModel[]): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/${itemId}/dept/${deptId}/employee/append`,
                method: 'PUT',
                data: emplItems
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static removeItemEmployee(id: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/relation/${id}`,
                method: 'DELETE',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static selectItemEmployeeByDeptId(deptId: string, itemId: string): Promise<QueueItemEmplModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/reg_item/${itemId}/depart/${deptId}/relation`,
                method: 'Get',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data ? res.data.map(item => new QueueItemEmplModel().deserialize(item)) : [])
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }
}
